package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by Administrator on 2019/7/9.
 */
@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions",regionService.queryHotRegion());
        return "destination/index";
    }
    //
    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){
        List<Destination> list = destinationService.getHotDestByRegionId(regionId);
        model.addAttribute("regionId",regionId);
        //leftDests
        model.addAttribute("leftDests",list.subList(0,list.size()/2+1));
        //rightDests
        model.addAttribute("rightDests",list.subList(list.size()/2+1,list.size()));
        return "destination/hotdestTpl";
    }
    @RequestMapping("guide")
    public String guide(Long id,Model model){
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.get(toasts.size() - 1);
        toasts.remove(dest);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.listByDestId(id));

        //strategyDetails:3个
        model.addAttribute("strategyDetails", strategyDetailService.getDetailTop3(id));
        return "destination/guide";
    }
    @RequestMapping("surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.get(toasts.size() - 1);
        toasts.remove(dest);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "destination/survey";
    }
    @RequestMapping("survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //catalogs
        //查询所有分类
        model.addAttribute("catalogs",strategyCatalogService.listByDestId(qo.getDestId()));
        //catalog
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);
        //detail
        //默认第一个
        if(catalog!=null && catalog.getDetails()!=null && catalog.getDetails().size()>0){
            //默认查看分类下面的第一个明细
            StrategyDetail strategyDetail = catalog.getDetails().get(0);
            StrategyContent strategyContent = strategyContentService.get(strategyDetail.getId());
            strategyDetail.setStrategyContent(strategyContent);
            model.addAttribute("detail",strategyDetail);
        }
        return "destination/surveyTpl";
    }
    @RequestMapping("travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo){
        model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }
}
